package com.central.apps.handler.workorder;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 处理工单业务逻辑
 *
 * @author lidachao
 * @date 2022/7/28
 */
@Component
@Slf4j
public class WorkOrderHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        String jpnum = MapUtils.getString(data,"jpnum");
        String wonum = MapUtils.getString(data,"wonum");
        log.info("-----------------------执行：" +jpnum+ "," +wonum+ " -----------------------");
        List<Map<String, Object>> datas = new ArrayList<>();

        // 1.查询作业任务信息
        List<Map<String, Object>> list = handlerService.findBySql("select jobtask, abstract from gq_jobtask t where t.jpnum = '" + jpnum + "'");

        if (CollectionUtils.isNotEmpty(list)) {
            log.info("-----------------------执行方法-----------------------");
            handlerService.updateBySql("delete from gq_wptask where wonum = '" + wonum + "'");

            // 组织插入数据
            for (Map<String, Object> map : list) {
                Map<String, Object> insertMap = new HashMap<>();

                insertMap.put("tasknum", map.get("jobtask"));// 任务编码
                insertMap.put("taskdesc", map.get("abstract"));// 任务描述
                insertMap.put("wonum", wonum);// 工单编码

                datas.add(insertMap);
            }

            // 插入作业任务信息
            handlerService.batchInsert("wptaskApp", "gq_wptask", datas);
        }

        // 2.查询作业计划-人员信息
        list = handlerService.findBySql("select labor, quantity, craft from gq_joblabor t where t.jpnum = '" + jpnum + "'");

        // 组织插入数据
        if (CollectionUtils.isNotEmpty(list)) {
            handlerService.updateBySql("delete from gq_wplabor where wonum = '" + wonum + "'");

            datas = new ArrayList<>();
            for (Map<String, Object> map : list) {
                Map<String, Object> insertMap = new HashMap<>();

                insertMap.put("labor", map.get("labor"));// 员工ID
                insertMap.put("quantity", map.get("quantity"));// 数量
                insertMap.put("craft", map.get("craft"));// 工种
                insertMap.put("wonum", wonum);// 工单编码

                datas.add(insertMap);
            }

            // 插入作业人员信息
            handlerService.batchInsert("wplaborApp", "gq_wplabor", datas);
        }

        // 3.查询作业计划-物料信息
        list = handlerService.findBySql("select itemnum, itemdesc, itemqty, unitcost, linecost from gq_jobitem t where t.jpnum = '" + jpnum + "'");

        // 组织插入数据
        if (CollectionUtils.isNotEmpty(list)) {
            handlerService.updateBySql("delete from gq_wpmaterial where wonum = '" + wonum + "'");

            datas = new ArrayList<>();
            for (Map<String, Object> map : list) {
                Map<String, Object> insertMap = new HashMap<>();

                insertMap.put("itemnum", map.get("itemnum"));// 物料编码
                insertMap.put("itemdesc", map.get("itemdesc"));// 物料名称
                insertMap.put("itemqty", map.get("itemqty"));// 数量
                insertMap.put("unitcost", map.get("unitcost"));// 单位成本
                insertMap.put("linecost", map.get("linecost"));// 行成本
                insertMap.put("wonum", wonum);// 工单编码

                datas.add(insertMap);
            }

            // 插入作业人员信息
            handlerService.batchInsert("wpmaterialApp", "gq_wpmaterial", datas);
        }
    }

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if(flowNode.getId().equals("complete")){
            // 查询工单信息
            String woSql = "select wonum, wotype from gq_workorder where id = " + id;
            List<Map<String, Object>> woList = handlerService.findBySql(woSql);

            if (CollectionUtils.isNotEmpty(woList)) {
                Map woMap = woList.get(0);
                String wonum = (String) woMap.get("wonum");
                String wotype = (String) woMap.get("wotype");

                // 处理员工提报和故障提报, 1:故障工单, 2:缺陷工单
                StringBuilder submitSql = new StringBuilder();
                submitSql.append("select submitnum from gq_submission where wonum = '" + wonum + "'");
                if (wotype.equals("1")) {
                    submitSql.append(" and type = '1'");
                } else {
                    submitSql.append(" and type = '2'");
                }

                List<Map<String, Object>> submitList = handlerService.findBySql(submitSql.toString());

                if (CollectionUtils.isNotEmpty(submitList)) {
                    for (Map<String, Object> map : submitList) {
                        String submitnum = (String) map.get("submitnum");

                        StringBuilder updateSql = new StringBuilder();
                        updateSql.append("update gq_submission set status = '5' where submitnum = '" + submitnum + "'");

                        handlerService.updateBySql(updateSql.toString());
                    }
                }
            }
        }
    }
}
